#!/usr/bin/env bash

set -e

if [ -z "$DEPENDABOT_NATIVE_HELPERS_PATH" ]; then
  echo "Unable to build, DEPENDABOT_NATIVE_HELPERS_PATH is not set"
  exit 1
fi

install_dir="$DEPENDABOT_NATIVE_HELPERS_PATH/composer/v2"
mkdir -p "$install_dir"

helpers_dir="$(dirname "${BASH_SOURCE[0]}")"
cp -r \
  "$helpers_dir/bin" \
  "$helpers_dir/src" \
  "$helpers_dir/.php-cs-fixer.php" \
  "$helpers_dir/composer.json" \
  "$helpers_dir/composer.lock" \
  "$helpers_dir/phpstan.dist.neon" \
  "$install_dir"

cd "$install_dir"

composer validate --no-check-publish
composer install
composer run lint -- --dry-run
composer run stan

# Composer caches source zips and repo metadata, none of which is useful. Save space in this layer
rm -Rf ~/.composer/cache
